<template>
  <div class="page">
    <div class="title">
      <div class="icon">
        <img src="@/assets/logo.png" alt="">
      </div>
      <div class="name">Baby Baby Love</div>
    </div>
    <div class="const">
      <div class="lt">
        <div class="up" @click="onClickRight(1)">
          <div class="lf">
            <div class="suanli">
              <div class="dont">动态算力</div>
              <van-icon class="arrow" size="14" name="arrow" />
            </div>
            <div class="val">{{ indexPage.dynamicPower }}</div>
          </div>
          <div class="img">
            <img src="@/assets/myPage.png" alt="">
          </div>
        </div>
        <div class="down van-hairline--top">
          <div class="name">全网动态算力</div>
          <div class="val">{{ indexPage.totalDynamicPower }}</div>
        </div>
      </div>
      <div class="lt">
        <div class="up" @click="onClickRight(0)">
          <div class="lf">
            <div class="suanli">
              <div class="dont">静态算力</div>
              <van-icon class="arrow" size="14" name="arrow" />
            </div>
            <div class="val">{{ indexPage.staticPower }}</div>
          </div>
          <div class="img">
            <img src="@/assets/myPage.png" alt="">
          </div>
        </div>
        <div class="down van-hairline--top">
          <div class="name">全网静态算力</div>
          <div class="val">{{ indexPage.totalStaticPower }}</div>
        </div>
      </div>
    </div>
    <div class="bosAdd">
      <div class="jijin">
        <div class="name">基金会地址</div>
        <div class="address">{{ proxy.plusXing(indexPage.marketingAddress, 4, 6, '.', 4) }}</div>
      </div>
      <div class="copy" @click="proxy.copy(indexPage.marketingAddress)">复制</div>
    </div>
    <div class="bosAdd">
      <div class="jijin">
        <div class="name">父级地址</div>
        <div class="address" v-if="parentAddress">{{ proxy.plusXing(parentAddress, 4, 6, '.', 4) }}</div>
        <div class="address" v-else>
          <van-field v-model="inputParentAddress" label="" placeholder="请输入父级地址" />
        </div>
      </div>
      <div class="copy" v-if="parentAddress" @click="proxy.copy(parentAddress)">复制</div>
      <div class="copy" v-else @click="bindAddress">绑定</div>
    </div>
    <div class="tuijian">
      <div class="tNum">
        <div class="tName">推荐人数</div>
        <div class="nums">
          <div class="n">{{ total }}人</div>
          <van-icon name="arrow-down" size="15" color="#ffffff" />
        </div>
      </div>
      <div class="items">
        <van-list v-model:loading="loading" :immediate-check="false" :finished="finished" finished-text="没有更多了"
          @load="onLoad">
          <div class="item" v-for="(item, index) in list" :key="index">
            <div class="leabel">{{ proxy.plusXing(item.address, 4, 6, '.', 4) }}</div>
            <div class="value">{{ item.dynamicPower + item.staticPower || 0 }}算力</div>
          </div>
        </van-list>
      </div>
    </div>
    <div class="zhiya" v-if="powerList.length">
      <div class="title">星级算力奖励</div>
      <div class="item" v-for="(item, index) in powerList" :key="index">
        <div class="leabel">{{ index + 1 }}星算力奖励</div>
        <div class="value">{{ item || 0 }}</div>
      </div>
    </div>
    <div class="zhiya">
      <div class="item">
        <div class="leabel">团队算力</div>
        <div class="value">{{ indexPage.teamPower }}</div>
      </div>
      <div class="item">
        <div class="leabel">邀请链接</div>
        <div class="value" @click="proxy.copy(inviter)">{{ proxy.plusXing(inviter, 8, 10, '.', 4) }}</div>
      </div>
      <!-- <div class="item">
        <div class="leabel">邀请二维码</div>
        <div class="value" @click="showCodeFn">点击查看二维码</div>
      </div> -->
      <van-button class="shouquan" block color="linear-gradient(90deg, #52A952, #40A5DB)" @click="userVote"
        :disabled="!vote">社区投票</van-button>
    </div>
  </div>
  <van-popup v-model:show="show" closeable round>
    <div class="box">
      <div class="time">社区投票</div>
      <div class="img">
        <img src="@/assets/toupiao.png" alt="">
      </div>
      <div class="tipsMain">
        <div class="item">
          <div class="leabel">当前算力排名</div>
          <div class="value">{{ voteData?.ranking }}名</div>
        </div>
        <div class="item">
          <div class="leabel">是否在投票时间</div>
          <div class="value">是</div>
        </div>
        <div class="item colum">
          <div class="leabel">选择投票类型</div>
          <div class="value choose">
            <div class="btn" v-for="(item, index) in btnList" :key="index" :class="active == (index + 1) ? 'active' : ''"
              @click="active = item.value">{{ item.name }}</div>
          </div>
        </div>
      </div>
      <van-button class="shouquan" block color="linear-gradient(90deg, #52A952, #40A5DB)" @click="userVoteTp">{{
        voteData?.type ? '已投票' : '提交投票' }}</van-button>
    </div>
  </van-popup>
  <van-popup v-model:show="showCode">
    <div id="code"></div>
  </van-popup>
  <Tabbar :active="4"></Tabbar>
</template>
<script setup name="index" lang="ts">
import Tabbar from '@@/Tabbar';
import { reactive, ref, onMounted, getCurrentInstance, computed } from 'vue';
import { addressIndex, addressChildren, confVote, vote, voteTypes, addressSelf, isLoopInviter, starPower } from '@/api/request'
import contractStore from '@/store/modules/contract'
import { useRouter, useRoute } from "vue-router";
import { Toast, Dialog } from 'vant'
import QRCode from 'qrcodejs2-fix';

interface listType {
  staticPower: number,
  dynamicPower: number,
  address: string,
}
interface voteType {
  address: string,
  cycle: number,
  type: number,
  ranking: number,
  createdTime: string
}
const list = ref<listType[]>([]);
const parentAddress = ref('')
const inputParentAddress = ref('')
const page = ref(1)
const loading = ref(false);
const finished = ref(false);
const total = ref(0)
const router = useRouter();
const route = useRoute();
const voteStatus = ref(false)
const voteData = ref<voteType>()
const { proxy } = getCurrentInstance() as any;
const appStore = contractStore()
const active = ref(2)
const powerList = ref([])
const show = ref(false)
const showCode = ref(false)
const address = computed(() => appStore.address)
const inviter = computed(() => location.origin + '/#/?address=' + address.value)
const indexPage = ref({
  "staticPower": 0,
  "dynamicPower": 0,
  "totalStaticPower": 0,
  "totalDynamicPower": 0,
  "marketingAddress": "",
  "teamPower": 0
})
onMounted(() => {
  proxy.userAuthorization(appStore, (res: boolean) => {
    if (res) {
      init()
      let result = route.query.isShow
      if(result) {
        Dialog.alert({
        title: '提示',
        message: '请先绑定父级地址',
        confirmButtonText: '确认',
        confirmButtonColor: '#49b2e7'
      }).then(() => {
        // on close
        
      });
      }
    }
  })
})
const bindAddress = async () => {
  if (inputParentAddress.value) {
    let res = await isLoopInviter(inputParentAddress.value)
    if (res.status == 200) {
      Toast(res.message || '绑定成功')
      init()
    } else {
      Toast(res.message || '绑定失败')
    }
  }
}
const showCodeFn = () => {
  showCode.value = true
  setTimeout(() => {
    let dom = document.getElementById('code') as HTMLInputElement
    dom.innerHTML = ""
    new QRCode(dom, {
      text: inviter.value
    });
  }, 100)
}
const onLoad = async () => {
  let res = await addressChildren({ page: page.value })
  let data = res.data
  if (res.status == 200) {
    total.value = data.total
    if (page.value == 1) {
      list.value = []
    }
    loading.value = false;
    list.value = list.value.concat(data.records)
    if (data.records.length < 20) {
      finished.value = true;
    } else {
      page.value++
    }
  }
}
const init = async () => {
  let res = await addressIndex()
  if (res.status == 200) {
    indexPage.value = res.data
  }
  onLoad()
  let val = await confVote()
  if (val.status == 200) {
    voteStatus.value = val.data
  }
  let ret = await addressSelf()
  if (ret.status == 200) {
    parentAddress.value = ret.data.parent
  }
  let rep = await starPower()
  if (rep.status == 200) {
    powerList.value = rep.data
  }
}
const userVote = async () => {
  let res = await vote()
  if (res.status == 200 && res.data) {
    voteData.value = res.data
    active.value = res.data.type ? res.data.type : 2
    show.value = true
  } else {
    Toast('暂无权限')
  }
}
const onClickRight = (val: number) => {
  router.push({
    path: 'power',
    query: {
      type: val
    }
  })
};
const userVoteTp = async () => {
  let res = await voteTypes(active.value)
  if (res.status == 200) {
    show.value = false
    Toast('投票成功')
  }
}
const btnList = reactive([
  {
    value: 1,
    name: '增产'
  },
  {
    value: 2,
    name: '减产'
  },
  {
    value: 3,
    name: '不变'
  },
])

</script>
<style scoped lang="less">
.box {
  display: flex;
  flex-direction: column;
  padding: 20px 24px;

  .tipsMain {
    display: flex;
    flex-direction: column;
    font-size: 18px;
    font-family: PingFang SC;
    font-weight: 400;
    color: #000000;
    margin-bottom: 5px;

    .item:first-child {
      margin-top: 0;
    }

    .item {
      display: flex;
      justify-content: space-between;
      margin-top: 22px;

      .leabel {
        font-size: 17px;
        font-family: PingFang SC;
        font-weight: 400;
        color: #6E6E6E;
      }

      .value {
        font-size: 18px;
        font-family: PingFang SC;
        font-weight: 500;
        color: #000000;
      }

      .choose {
        display: flex;
        align-items: center;

        .btn {
          height: 32px;
          line-height: 1;
          display: flex;
          align-items: center;
          background: #EEEEEE;
          border-radius: 16px;
          padding: 0 13px;
          font-size: 17px;
          font-family: PingFang SC;
          font-weight: 400;
          color: #838383;
          margin-right: 7px;
        }

        .btn:last-child {
          margin-right: 0;
        }

        .active {
          background: #47A7A4;
          color: #FFFFFF;
        }
      }
    }

    .colum {
      flex-direction: column;

      .choose {
        justify-content: space-around;
        margin-top: 20px;
      }
    }
  }

  .img {
    width: 155px;
    height: 120px;
    margin: 18px 52px;
  }

  .time {
    font-size: 25px;
    font-family: PingFang SC;
    font-weight: 800;
    color: #000000;
  }
}

.page {
  background: #E9E9E9;
  min-height: calc(100vh - 50px);
  padding: 0 13px;
  padding-bottom: 50px;

  .tuijian {
    background: #FFFFFF;
    border-radius: 15px;
    padding: 20px;

    .items {
      margin-top: 14px;
      max-height: 100px;
      overflow-y: scroll;

      .item {
        display: flex;
        justify-content: space-around;

        .leabel {
          font-size: 13px;
          font-family: PingFang SC;
          font-weight: 400;
          color: #575757;
          line-height: 24px;
        }

        .value {
          font-size: 13px;
          font-family: PingFang SC;
          font-weight: 400;
          color: #575757;
          line-height: 24px;
        }
      }
    }

    .tNum {
      height: 36px;
      background: #454545;
      border-radius: 10px;
      display: flex;
      justify-content: space-between;
      padding: 0 17px;
      align-items: center;

      .tName {
        font-size: 15px;
        font-family: PingFang SC;
        font-weight: 500;
        color: #FFFFFF;
      }

      .nums {
        display: flex;
        align-items: center;

        .n {
          margin-right: 5px;
          font-size: 15px;
          font-family: PingFang SC;
          font-weight: 500;
          color: #FFFFFF;
        }
      }
    }
  }

  .bosAdd {
    display: flex;
    align-items: center;
    margin-top: 16px;
    margin-bottom: 15px;

    .copy {
      height: 44px;
      background: #47A7A4;
      border-radius: 15px;
      padding: 0 20px;
      font-size: 19px;
      font-family: PingFang SC;
      font-weight: 500;
      color: #FFFFFF;
      line-height: 44px;
    }

    .jijin {
      height: 44px;
      background: #FFFFFF;
      border-radius: 15px;
      flex: 1;
      margin-right: 12px;
      padding: 0 13px;
      display: flex;
      align-items: center;

      .name {
        font-size: 13px;
        font-family: PingFang SC;
        font-weight: 400;
        color: #575757;
        margin-right: 14px;
        white-space: pre;
      }

      .address {
        font-size: 15px;
        font-family: PingFang SC;
        font-weight: 800;
        color: #000000;

        .van-cell {
          padding-left: 0;
          padding-right: 0;
        }
      }
    }
  }

  .const {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    column-gap: 14px;

    .lt {
      display: flex;
      flex-direction: column;
      background: #FFFFFF;
      border-radius: 15px;
      padding: 16px 13px 17px 16px;

      .down {
        display: flex;
        flex-direction: column;

        .name {
          font-size: 13px;
          font-family: PingFang SC;
          font-weight: 400;
          color: #575757;
          margin-top: 18px;
        }

        .val {
          margin-top: 11px;
          font-size: 17px;
          font-family: PingFang SC;
          font-weight: 500;
          color: #000000;
        }
      }

      .up {
        display: flex;
        align-items: center;
        margin-bottom: 13px;
        justify-content: space-between;

        .img {
          width: 50px;
          height: 42px;
        }

        .lf {
          display: flex;
          flex-direction: column;

          .suanli {
            display: flex;
            align-items: center;

            .dont {
              font-size: 13px;
              font-family: PingFang SC;
              font-weight: 400;
              color: #575757;
              line-height: 28px;
            }
          }
        }
      }
    }
  }

  .zhiya {
    margin-top: 15px;
    background: #FFFFFF;
    border-radius: 15px;
    padding: 20px;

    .title {
      padding-top: 0 !important;
      font-size: 16px;
    }

    .item {
      line-height: 28px;
      display: flex;
      justify-content: space-between;

      .leabel {
        font-size: 15px;
        font-family: PingFang SC;
        font-weight: 400;
        color: #767676;
      }

      .value {
        font-size: 15px;
        font-family: PingFang SC;
        font-weight: 800;
        color: #000000;
      }
    }
  }


  .title {
    display: flex;
    align-items: center;
    justify-content: center;
    padding-top: 15px;
    padding-bottom: 15px;

    .icon {
      width: 48px;
      height: 48px;
      margin-right: 6px;
    }

    .name {
      font-size: 19px;
      font-family: PingFang SC;
      font-weight: 800;
      color: #000000;
    }
  }
}
</style>